package co.edu.usbcali.presentation.backingBeans;

import co.edu.usbcali.exceptions.*;
import co.edu.usbcali.modelo.*;
import co.edu.usbcali.modelo.dto.SolicitudDTO;
import co.edu.usbcali.presentation.businessDelegate.*;
import co.edu.usbcali.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.RowEditEvent;

import java.io.Serializable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
@ManagedBean
@ViewScoped
public class SolicitudView implements Serializable {
    private static final long serialVersionUID = 1L;
    private InputText txtEstado;
    private InputText txtMedio;
    private InputText txtMontosolicitado;
    private InputText txtIdpers_Persona;
    private InputText txtIdsoli;
    private Calendar txtFecha;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<SolicitudDTO> data;
    private SolicitudDTO selectedSolicitud;
    private Solicitud entity;
    private boolean showDialog;
    @ManagedProperty(value = "#{BusinessDelegatorView}")
    private IBusinessDelegatorView businessDelegatorView;

    public SolicitudView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            SolicitudDTO solicitudDTO = (SolicitudDTO) e.getObject();

            if (txtEstado == null) {
                txtEstado = new InputText();
            }

            txtEstado.setValue(solicitudDTO.getEstado());

            if (txtMedio == null) {
                txtMedio = new InputText();
            }

            txtMedio.setValue(solicitudDTO.getMedio());

            if (txtMontosolicitado == null) {
                txtMontosolicitado = new InputText();
            }

            txtMontosolicitado.setValue(solicitudDTO.getMontosolicitado());

            if (txtIdpers_Persona == null) {
                txtIdpers_Persona = new InputText();
            }

            txtIdpers_Persona.setValue(solicitudDTO.getIdpers_Persona());

            if (txtIdsoli == null) {
                txtIdsoli = new InputText();
            }

            txtIdsoli.setValue(solicitudDTO.getIdsoli());

            if (txtFecha == null) {
                txtFecha = new Calendar();
            }

            txtFecha.setValue(solicitudDTO.getFecha());

            Long idsoli = FacesUtils.checkLong(txtIdsoli);
            entity = businessDelegatorView.getSolicitud(idsoli);

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_new() {
        action_clear();
        selectedSolicitud = null;
        setShowDialog(true);

        return "";
    }

    public String action_clear() {
        entity = null;
        selectedSolicitud = null;

        if (txtEstado != null) {
            txtEstado.setValue(null);
            txtEstado.setDisabled(true);
        }

        if (txtMedio != null) {
            txtMedio.setValue(null);
            txtMedio.setDisabled(true);
        }

        if (txtMontosolicitado != null) {
            txtMontosolicitado.setValue(null);
            txtMontosolicitado.setDisabled(true);
        }

        if (txtIdpers_Persona != null) {
            txtIdpers_Persona.setValue(null);
            txtIdpers_Persona.setDisabled(true);
        }

        if (txtFecha != null) {
            txtFecha.setValue(null);
            txtFecha.setDisabled(true);
        }

        if (txtIdsoli != null) {
            txtIdsoli.setValue(null);
            txtIdsoli.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        return "";
    }

    public void listener_txtFecha() {
        Date inputDate = (Date) txtFecha.getValue();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        FacesContext.getCurrentInstance()
                    .addMessage("",
            new FacesMessage("Selected Date " + dateFormat.format(inputDate)));
    }

    public void listener_txtId() {
        try {
            Long idsoli = FacesUtils.checkLong(txtIdsoli);
            entity = (idsoli != null)
                ? businessDelegatorView.getSolicitud(idsoli) : null;
        } catch (Exception e) {
            entity = null;
        }

        if (entity == null) {
            txtEstado.setDisabled(false);
            txtMedio.setDisabled(false);
            txtMontosolicitado.setDisabled(false);
            txtIdpers_Persona.setDisabled(false);
            txtFecha.setDisabled(false);
            txtIdsoli.setDisabled(false);
            btnSave.setDisabled(false);
        } else {
            txtEstado.setValue(entity.getEstado());
            txtEstado.setDisabled(false);
            txtFecha.setValue(entity.getFecha());
            txtFecha.setDisabled(false);
            txtMedio.setValue(entity.getMedio());
            txtMedio.setDisabled(false);
            txtMontosolicitado.setValue(entity.getMontosolicitado());
            txtMontosolicitado.setDisabled(false);
            txtIdpers_Persona.setValue(entity.getPersona().getIdpers());
            txtIdpers_Persona.setDisabled(false);
            txtIdsoli.setValue(entity.getIdsoli());
            txtIdsoli.setDisabled(true);
            btnSave.setDisabled(false);

            if (btnDelete != null) {
                btnDelete.setDisabled(false);
            }
        }
    }

    public String action_edit(ActionEvent evt) {
        selectedSolicitud = (SolicitudDTO) (evt.getComponent().getAttributes()
                                               .get("selectedSolicitud"));
        txtEstado.setValue(selectedSolicitud.getEstado());
        txtEstado.setDisabled(false);
        txtFecha.setValue(selectedSolicitud.getFecha());
        txtFecha.setDisabled(false);
        txtMedio.setValue(selectedSolicitud.getMedio());
        txtMedio.setDisabled(false);
        txtMontosolicitado.setValue(selectedSolicitud.getMontosolicitado());
        txtMontosolicitado.setDisabled(false);
        txtIdpers_Persona.setValue(selectedSolicitud.getIdpers_Persona());
        txtIdpers_Persona.setDisabled(false);
        txtIdsoli.setValue(selectedSolicitud.getIdsoli());
        txtIdsoli.setDisabled(true);
        btnSave.setDisabled(false);
        setShowDialog(true);

        return "";
    }

    public String action_save() {
        try {
            if ((selectedSolicitud == null) && (entity == null)) {
                action_create();
            } else {
                action_modify();
            }

            data = null;
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_create() {
        try {
            entity = new Solicitud();

            Long idsoli = FacesUtils.checkLong(txtIdsoli);

            entity.setEstado(FacesUtils.checkString(txtEstado));
            entity.setFecha(FacesUtils.checkDate(txtFecha));
            entity.setIdsoli(idsoli);
            entity.setMedio(FacesUtils.checkString(txtMedio));
            entity.setMontosolicitado(FacesUtils.checkFloat(txtMontosolicitado));
            entity.setPersona((FacesUtils.checkLong(txtIdpers_Persona) != null)
                ? businessDelegatorView.getPersona(FacesUtils.checkLong(
                        txtIdpers_Persona)) : null);
            businessDelegatorView.saveSolicitud(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            entity = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            if (entity == null) {
                Long idsoli = new Long(selectedSolicitud.getIdsoli());
                entity = businessDelegatorView.getSolicitud(idsoli);
            }

            entity.setEstado(FacesUtils.checkString(txtEstado));
            entity.setFecha(FacesUtils.checkDate(txtFecha));
            entity.setMedio(FacesUtils.checkString(txtMedio));
            entity.setMontosolicitado(FacesUtils.checkFloat(txtMontosolicitado));
            entity.setPersona((FacesUtils.checkLong(txtIdpers_Persona) != null)
                ? businessDelegatorView.getPersona(FacesUtils.checkLong(
                        txtIdpers_Persona)) : null);
            businessDelegatorView.updateSolicitud(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_datatable(ActionEvent evt) {
        try {
            selectedSolicitud = (SolicitudDTO) (evt.getComponent()
                                                   .getAttributes()
                                                   .get("selectedSolicitud"));

            Long idsoli = new Long(selectedSolicitud.getIdsoli());
            entity = businessDelegatorView.getSolicitud(idsoli);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete_master() {
        try {
            Long idsoli = FacesUtils.checkLong(txtIdsoli);
            entity = businessDelegatorView.getSolicitud(idsoli);
            action_delete();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public void action_delete() throws Exception {
        try {
            businessDelegatorView.deleteSolicitud(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data = null;
        } catch (Exception e) {
            throw e;
        }
    }

    public String action_closeDialog() {
        setShowDialog(false);
        action_clear();

        return "";
    }

    public String actionDeleteDataTableEditable(ActionEvent evt) {
        try {
            selectedSolicitud = (SolicitudDTO) (evt.getComponent()
                                                   .getAttributes()
                                                   .get("selectedSolicitud"));

            Long idsoli = new Long(selectedSolicitud.getIdsoli());
            entity = businessDelegatorView.getSolicitud(idsoli);
            businessDelegatorView.deleteSolicitud(entity);
            data.remove(selectedSolicitud);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String estado, Date fecha, Long idsoli,
        String medio, Float montosolicitado, Long idpers_Persona)
        throws Exception {
        try {
            entity.setEstado(FacesUtils.checkString(estado));
            entity.setFecha(FacesUtils.checkDate(fecha));
            entity.setMedio(FacesUtils.checkString(medio));
            entity.setMontosolicitado(FacesUtils.checkFloat(montosolicitado));
            businessDelegatorView.updateSolicitud(entity);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("SolicitudView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtEstado() {
        return txtEstado;
    }

    public void setTxtEstado(InputText txtEstado) {
        this.txtEstado = txtEstado;
    }

    public InputText getTxtMedio() {
        return txtMedio;
    }

    public void setTxtMedio(InputText txtMedio) {
        this.txtMedio = txtMedio;
    }

    public InputText getTxtMontosolicitado() {
        return txtMontosolicitado;
    }

    public void setTxtMontosolicitado(InputText txtMontosolicitado) {
        this.txtMontosolicitado = txtMontosolicitado;
    }

    public InputText getTxtIdpers_Persona() {
        return txtIdpers_Persona;
    }

    public void setTxtIdpers_Persona(InputText txtIdpers_Persona) {
        this.txtIdpers_Persona = txtIdpers_Persona;
    }

    public Calendar getTxtFecha() {
        return txtFecha;
    }

    public void setTxtFecha(Calendar txtFecha) {
        this.txtFecha = txtFecha;
    }

    public InputText getTxtIdsoli() {
        return txtIdsoli;
    }

    public void setTxtIdsoli(InputText txtIdsoli) {
        this.txtIdsoli = txtIdsoli;
    }

    public List<SolicitudDTO> getData() {
        try {
            if (data == null) {
                data = businessDelegatorView.getDataSolicitud();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<SolicitudDTO> solicitudDTO) {
        this.data = solicitudDTO;
    }

    public SolicitudDTO getSelectedSolicitud() {
        return selectedSolicitud;
    }

    public void setSelectedSolicitud(SolicitudDTO solicitud) {
        this.selectedSolicitud = solicitud;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public IBusinessDelegatorView getBusinessDelegatorView() {
        return businessDelegatorView;
    }

    public void setBusinessDelegatorView(
        IBusinessDelegatorView businessDelegatorView) {
        this.businessDelegatorView = businessDelegatorView;
    }

    public boolean isShowDialog() {
        return showDialog;
    }

    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }
}
